//===============================================================================
// Microsoft patterns & practices
// Smart Client Software Factory 2010
//===============================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
//----------------------------------------------------------------------------------------
// patterns & practices - Smart Client Software Factory - Guidance Package
//
// This file was generated by the "Add View" recipe.
//
// A presenter calls methods of a view to update the information that the view displays. 
// The view exposes its methods through an interface definition, and the presenter contains
// a reference to the view interface. This allows you to test the presenter with different 
// implementations of a view (for example, a mock view).
//
// For more information see:
// ms-help://MS.VSCC.v80/MS.VSIPCC.v80/ms.scsf.2006jun/SCSF/html/03-030-Model%20View%20Presenter%20%20MVP%20.htm
//
// Latest version of this Guidance Package: http://go.microsoft.com/fwlink/?LinkId=62182
//----------------------------------------------------------------------------------------

using System;
using GlobalBank.BranchSystems.Interface.Constants;
using GlobalBank.BranchSystems.Interface.Services;
using GlobalBank.Infrastructure.Interface;
using GlobalBank.Infrastructure.Interface.BusinessEntities;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.EventBroker;
using Microsoft.Practices.ObjectBuilder;

namespace GlobalBank.BranchSystems.Module.Views
{
	public class AddVisitorToQueueViewPresenter : Presenter<IAddVisitorToQueueView>
	{
		private ICustomerQueueService _queueService;
		private WalkIn _walkIn = null;

		[EventPublication(EventTopicNames.PersonAddedToQueue, PublicationScope.Global)]
		public event EventHandler<EventArgs<Person>> PersonAddedToQueue;

		[InjectionConstructor]
		public AddVisitorToQueueViewPresenter
			(
			[ServiceDependency] ICustomerQueueService queueService
			)
		{
			_queueService = queueService;
		}

		private Customer Customer
		{
			get
			{
				if (WorkItem == null) return null;
				if (WorkItem.Items.Contains("CustomerToAdd")) return WorkItem.Items.Get<Customer>("CustomerToAdd");
				return null;
			}
		}

		protected override void OnViewSet()
		{
			base.OnViewSet();

			if (WorkingMode == AddVisitorToQueueWorkingMode.AddVisitor)
			{
				_walkIn = CreateWalkin();
			}
		}

		public override void OnViewReady()
		{
			base.OnViewReady();

			switch (WorkingMode)
			{
				case AddVisitorToQueueWorkingMode.AddCustomer:
					View.ShowCustomer(Customer);
					break;

				case AddVisitorToQueueWorkingMode.AddVisitor:
					View.ShowWalkin(_walkIn);
					break;
			}
		}

		public AddVisitorToQueueWorkingMode WorkingMode
		{
			get { return Customer == null ? AddVisitorToQueueWorkingMode.AddVisitor : AddVisitorToQueueWorkingMode.AddCustomer; }
		}

		private WalkIn CreateWalkin()
		{
			WalkIn result = new WalkIn();
			result.Addresses = new Address[] {new Address()};
			result.EmailAddresses = new EmailAddress[] {new EmailAddress()};
			result.PhoneNumbers = new PhoneNumber[] {new PhoneNumber(), new PhoneNumber(), new PhoneNumber()};
			result.PhoneNumbers[0].PhoneType = PhoneType.Home;
			result.PhoneNumbers[1].PhoneType = PhoneType.Work;
			result.PhoneNumbers[2].PhoneType = PhoneType.Mobile;
			return result;
		}

		public void QueueForService(string reason, string description)
		{
			if (Customer != null)
			{
				try
				{
					_queueService.AddCustomer(Customer, reason, description);
					OnPersonAddedToQueue(Customer);
				}
				catch (DuplicateCustomerException)
				{
					View.NotifyCustomerAlreadyInQueue();
				}
			}
			else if (_walkIn != null)
			{
				_queueService.AddWalkin(_walkIn, reason, description);
				OnPersonAddedToQueue(_walkIn);
			}
			CloseView();
		}

		protected override void CloseView()
		{
			base.CloseView();

			if (WorkingMode == AddVisitorToQueueWorkingMode.AddCustomer)
				WorkItem.Terminate();
			else if (View is IDisposable)
				((IDisposable) View).Dispose();
		}

		protected virtual void OnPersonAddedToQueue(Person person)
		{
			if (PersonAddedToQueue != null)
				PersonAddedToQueue(this, new EventArgs<Person>(person));
		}
	}
}
